package com.eian.boot.module.system.permission.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.eian.boot.common.constants.PermissionConstants;
import com.eian.boot.common.core.model.CommonResult;
import com.eian.boot.module.system.permission.model.dto.CreatePermissionRequest;
import com.eian.boot.module.system.permission.model.dto.PermissionDeleteRequest;
import com.eian.boot.module.system.permission.model.dto.PermissionQueryRequest;
import com.eian.boot.module.system.permission.model.dto.PermissionUpdateRequest;
import com.eian.boot.module.system.permission.model.vo.PermissionTreeVO;
import com.eian.boot.module.system.permission.service.SysPermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "权限管理")
@RestController
@RequestMapping("/system/permission")
@AllArgsConstructor
public class PermissionController {

    private final SysPermissionService sysPermissionService;

    @Operation(summary = "创建权限")
    @SaCheckPermission(PermissionConstants.Permission.CREATE)
    @PostMapping("/create")
    public CommonResult<String> create(@Valid @RequestBody CreatePermissionRequest request) {
        sysPermissionService.createPermission(request);
        return CommonResult.ok();
    }

    @Operation(summary = "查询权限树列表")
    @SaCheckPermission(PermissionConstants.Permission.LIST)
    @GetMapping("/tree-list")
    public CommonResult<List<PermissionTreeVO>> tree(@Valid PermissionQueryRequest request) {
        return CommonResult.ok(sysPermissionService.getPermissionTree(request));
    }

    @Operation(summary = "获取权限详情")
    @SaCheckPermission(PermissionConstants.Permission.QUERY)
    @GetMapping("/{id}")
    public CommonResult<PermissionTreeVO> getDetail(@Parameter(description = "菜单ID") @PathVariable Long id) {
        return CommonResult.ok(sysPermissionService.getDetail(id));
    }

    @Operation(summary = "更新权限信息")
    @SaCheckPermission(PermissionConstants.Permission.UPDATE)
    @PostMapping("/update")
    public CommonResult<String> update(@Valid @RequestBody PermissionUpdateRequest request) {
        sysPermissionService.update(request);
        return CommonResult.ok("菜单信息更新成功");
    }

    @Operation(summary = "删除权限")
    @SaCheckPermission(PermissionConstants.Permission.DELETE)
    @PostMapping("/delete")
    public CommonResult<String> delete(@Valid @RequestBody PermissionDeleteRequest request) {
        sysPermissionService.delete(request);
        return CommonResult.ok("菜单删除成功");
    }
}
